package com.example.javabasic.thread.classtwo.threadlocal.demo5;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
//生产者线程
public class ProducerThread extends Thread {
    private final MessageQueue  messageQueue;
    //随机数
    private static final Random random = new Random(System.currentTimeMillis());
    //计数器
    private static final AtomicInteger counter = new AtomicInteger(0);
    public ProducerThread(MessageQueue messageQueue,int num) {
        super("producer"+num);
        this.messageQueue = messageQueue;
    }

    @Override
    public void run() {

        while(true){
            try {
                String data = "message"+counter.getAndIncrement();
                messageQueue.put(new Message(data));
                System.out.println(Thread.currentThread().getName()+"生产消息->"+data);
                Thread.sleep(random.nextInt(500));
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}
